{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c7723013",
   "metadata": {},
   "source": [
    "正常情况下，我们定义了一个class，创建了一个class的实例后，我们可以给该实例绑定任何属性和方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "22edd896",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student(object):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "80bb1d31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Bob'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = Student()\n",
    "b = Student()\n",
    "s.name = 'Bob'\n",
    "s.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2c4dea26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#给实例绑定一个方法\n",
    "def set_age(self,age):\n",
    "    self.age = age\n",
    "from types import MethodType\n",
    "s.set_age = MethodType(set_age, s)\n",
    "s.set_age(25)\n",
    "s.age"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3841e795",
   "metadata": {},
   "source": [
    "如果像实现给所有实例都绑定方法，可以给class绑定方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e80d2a2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Student.set_age = set_age\n",
    "s.set_age(100)\n",
    "s.age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d9124a9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "33"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.set_age(33)\n",
    "b.age"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "877ab6be",
   "metadata": {},
   "source": [
    "### 使用slots"
   ]
  },
  {
   "cell_type": "raw",
   "id": "03832bbe",
   "metadata": {},
   "source": [
    "如果我们像要限制实例的属性，比如，只允许Student实例添加name和age属性。\n",
    "为了达到限制的目的，python允许在顶i有class的时候，定义一个特殊的__slots__变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "94efa55a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student(object):\n",
    "    __slots__ = ('name', 'age') #用tuple定义允许绑定的属性名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0e1f152c",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Student' object has no attribute 'score'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_26884/1268494463.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'Bob'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m25\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscore\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m99\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'Student' object has no attribute 'score'"
     ]
    }
   ],
   "source": [
    "s = Student()\n",
    "s.name = 'Bob'\n",
    "s.age = 25\n",
    "s.score = 99"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68b89d41",
   "metadata": {},
   "source": [
    "由于score没有被放到slots中，所以不能绑定score属性，试图绑定score将得到错误"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3018bc71",
   "metadata": {},
   "source": [
    "slots定义的属性仅对带你给钱实例起作用，对进程的子类不起作用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fee2eeb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "99"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class GraduateStudent(Student):\n",
    "    pass\n",
    "g = GraduateStudent()\n",
    "g.score = 99\n",
    "g.score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b17a528f",
   "metadata": {},
   "source": [
    "除非子类中也定义slots，这样子类实例允许定义的属性就是自身的slots加上父类的slots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9af2cfe9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
